/**
 * Created by xuxujunjunforever on 2015/12/9.
 */
'use strict'

let models = require('../../../modules/models');
let config = require('../../../config');
let userActions = require('../../../modules/actions/user');
let Fibers = require('fibers');

let UserDao = models.UserDao;

class Response {
    json(obj) {
        this.result = obj;
    }
}
let session = {};

function actionLoginSuccessful(test) {
    new Fibers(function() {
        let req = {
            "body":{
                "user": {
                    "name": "test",
                    "password": "e10adc3949ba59abbe56e057f20f883e"
                }
            }
        };

        let res = new Response();

        userActions.actionLogin(req, res);

        test.ok(res.result.successful,'login failed');
        test.notStrictEqual(res.result.data.token,null,'token-making failed');

        test.done();
    }).run();
}

function actionLoginNotMatched(test){
    new Fibers(function() {
        let req = {
            "body":{
                "user": {
                    "name": "test",
                    "password": "1"
                }
            }
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionLogin(req, res);
        },config.errors.classes.UsernamePasswordMismatched);

        test.done();
    }).run();
}

function actionLogoutSuccessful(test){
    new Fibers(function() {
        let req = {
            "session":session
        };
        let res = new Response();

        userActions.actionLogout(req,res);

        test.ok(res.result.successful,'logout failed');

        test.done();
    }).run();

}

function actionLogoutNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}};
        let res = new Response();

        test.throws(function() {
            userActions.actionLogout(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();

}

function actionGetUserInfomationSuccessful(test){
    new Fibers(function() {
        let req = {
            "session":session
        };
        let res = new Response();

        userActions.actionGetUserInfomation(req,res);

        test.ok(res.result.successful,'GetUserInfomation failed');

        let userinfomation = userActions.makeUserInformation(session.login.user);

        test.deepEqual(res.result.data.user,userinfomation,'userInfomation do not right');

        test.done();

    }).run();
}

function actionGetUserInfomationNotLogin(test){
    new Fibers(function() {
        let req ={"session":{"login": null}};
        let res = new Response();

        test.throws(function() {
            userActions.actionGetUserInfomation(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();

}

function actionSwitchSchoolDoNotChange(test){
    new Fibers(function() {
        let req = {
            "body":{
                "school": {
                    "id": "5667cbd58e731ba021645249"
                }
            },
            "session":session
        };
        let res = new Response();

        let olduser = UserDao.findOne({
            id:session.login.user.id
        });

        userActions.actionSwitchSchool(req,res);

        test.ok(res.result.successful,'SwitchSchool failed');

        let newuser = UserDao.findOne({
            id:session.login.user.id
        });
        test.deepEqual(olduser.school.id,newuser.school.id,'SwitchSchool do not right');

        test.done();

    }).run();

}

function actionSwitchSchoolSuccessful(test){
    new Fibers(function() {
        let req = {
            "body":{
                "school": {
                    "id": "5667cbd58e731ba02164524a"
                }
            },
            "session":session
        };
        let res = new Response();

        userActions.actionSwitchSchool(req,res);

        test.ok(res.result.successful,'SwitchSchool failed');

        let user = UserDao.findOne({
            id:session.login.user.id
        });

        test.deepEqual(user.school.id,req.body.school.id,'SwitchSchool do not right');

        test.done();

    }).run();

}

function actionSwitchSchoolNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "school": {
                    "id": "5667cbd58e731ba021645240"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionSwitchSchool(req,res);
        },config.errors.classes.SchoolNotFound);

        test.done();
    }).run();

}

function actionSwitchSchoolNotLogin(test){
    new Fibers(function() {
        let req = {
            "body":{
                "school": {
                    "id": "5667cbd58e731ba021645249"
                }
            },
            "session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionSwitchSchool(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionCreateUserNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionCreateUser(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionCreateUserRoleNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "user":{
                    name: "june",
                    role: {id:"5667cbd68e731ba021645250"},
                    realName: "tom",
                    telephone: "123456789",
                    school:{id:"5667cbd58e731ba021645249"},
                    dormitory:"3#218",
                    password: "123456789",
                    status:1
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionCreateUser(req,res);
        },config.errors.classes.RoleCanNotFound);

        test.done();
    }).run();
}

function actionCreateUserSchoolNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "user":{
                    name: "june",
                    role: {id:"5667cbd68e731ba02164525b"},
                    realName: "tom",
                    telephone: "123456789",
                    school:{id:"5667cbd58e731ba021645240"},
                    dormitory:"3#218",
                    password: "123456789",
                    status:1
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionCreateUser(req,res);
        },config.errors.classes.SchoolNotFound);

        test.done();
    }).run();
}

function actionCreateUserSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "user": {
                    name: "june",
                    role: {id: "5667cbd68e731ba02164525b"},
                    realName: "tom",
                    telephone: "123456789",
                    school: {id: "5667cbd58e731ba021645249"},
                    dormitory: "3#218",
                    password: "123456789",
                    status: 1
                }
            },
            "session": session
        };
        let res = new Response();

        userActions.actionCreateUser(req, res);
        test.ok(res.result.successful,'creat failed')

        let user = UserDao.findOne({
            id:res.result.data.user.id
        });
        let issame = false;
        if (user.name == req.body.user.name &&
            user.role.id == req.body.user.role.id &&
            user.realName == req.body.user.realName &&
            user.telephone == req.body.user.telephone &&
            user.school.id == req.body.user.school.id &&
            user.dormitory == req.body.user.dormitory &&
            user.password == req.body.user.password &&
            user.status == req.body.user.status) {
            issame = true;
        }

        test.ok(issame,'they should be equal');

        test.done();
    }).run();
}

function actionRefreshUserNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionRefreshUser(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRefreshUserNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "user":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionRefreshUser(req,res);
        },config.errors.classes.UserCanNotFound);

        test.done();
    }).run();
}

function actionRefreshUserSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "user": {
                   "id":"5667cbd68e731ba02164525d"
                }
            },
            "session": session
        };
        let res = new Response();

        userActions.actionRefreshUser(req, res);
        test.ok(res.result.successful,'refresh failed')

        let user = UserDao.findOne({
            id:res.result.data.user.id
        });

        test.deepEqual(user.toObject({recursive:true}),res.result.data.user,'they should be equal');

        test.done();
    }).run();
}

function actionRemoveUserNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionRemoveUser(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRemoveUserNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "user":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            userActions.actionRemoveUser(req,res);
        },config.errors.classes.UserCanNotFound);

        test.done();
    }).run();
}

function actionRemoveUserSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "user": {
                   "id":"567773528b3eb6bae47446fb",
                }
            },
            "session": session
        };
        let res = new Response();

        userActions.actionRemoveUser(req, res);
        test.ok(res.result.successful,'refresh failed')

        let user = UserDao.findOne({
            id:req.body.user.id
        });

        test.deepEqual(user,null,'user should not be found');

        test.done();
    }).run();
}

module.exports = {
    setUp: function (callback){
        session = {
          "login":{
              "user": {
                  "id":"5667cbd68e731ba02164525d",
                  "name": "test",
                  "password": "e10adc3949ba59abbe56e057f20f883e"
              }
          }
        };
            callback();
    },
    //actionLoginSuccessful,
    //actionLoginNotMatched,
    //actionLogoutSuccessful,
    //actionLogoutNotLogin,
    //actionGetUserInfomationSuccessful,
    //actionGetUserInfomationNotLogin,
    ////actionSwitchSchoolDoNotChange,
    //actionSwitchSchoolSuccessful,
    //actionSwitchSchoolNotFound,
    //actionSwitchSchoolNotLogin,
    //actionCreateUserNotLogin,
    //actionCreateUserRoleNotFound,
    //actionCreateUserSchoolNotFound,
    //actionCreateUserSuccessful,
    //actionRefreshUserNotLogin,
    //actionRefreshUserNotFound,
    //actionRefreshUserSuccessful,
    //actionRemoveUserNotLogin,
    //actionRemoveUserNotFound,
    //actionRemoveUserSuccessful
};
